package book

import (
	"context"

	domainBook "BookStack/internal/domain/book"
	"BookStack/internal/domain/events"
)

// ApplicationService 书籍应用服务接口定义
type ApplicationService interface {
	// 书籍相关
	CreateBook(ctx context.Context, req *CreateBookRequest) (*BookResponse, error)
	GetBookByID(ctx context.Context, id uint) (*BookResponse, error)
	GetBookBySlug(ctx context.Context, slug string) (*BookResponse, error)
	UpdateBook(ctx context.Context, id uint, req *UpdateBookRequest, updatedBy uint) (*BookResponse, error)
	DeleteBook(ctx context.Context, id uint, deletedBy uint) error
	ListBooks(ctx context.Context, params *BookQueryParams) (*BookListResponse, error)
	PublishBook(ctx context.Context, id uint, updatedBy uint) (*BookResponse, error)
	SearchBooks(ctx context.Context, keyword string, page, pageSize int) (*BookListResponse, error)
	ToggleBookRecommend(ctx context.Context, id uint, isRecommend bool, updatedBy uint) (*BookResponse, error)
	ListUserBooks(ctx context.Context, userID uint, page, pageSize int) (*BookListResponse, error)
	IncrementBookView(ctx context.Context, id uint) error
	IncrementChapterView(ctx context.Context, id uint) error
	ToggleBookLike(ctx context.Context, id uint, userID uint) error
	ToggleBookStar(ctx context.Context, id uint, userID uint) error
	ListUserStarredBooks(ctx context.Context, userID uint, page, pageSize int) (*BookListResponse, error)
	IsUserLikedBook(ctx context.Context, bookID, userID uint) (bool, error)
	IsUserStarredBook(ctx context.Context, bookID, userID uint) (bool, error)

	// 章节相关
	CreateChapter(ctx context.Context, req *CreateChapterRequest) (*ChapterResponse, error)
	GetChapterByID(ctx context.Context, id uint) (*ChapterResponse, error)
	UpdateChapter(ctx context.Context, id uint, req *UpdateChapterRequest, updatedBy uint) (*ChapterResponse, error)
	DeleteChapter(ctx context.Context, id uint, deletedBy uint) error
	ListChaptersByBookID(ctx context.Context, bookID uint) ([]*ChapterResponse, error)
	ReorderChapters(ctx context.Context, req *ChapterReorderRequest, updatedBy uint) error
	GetPrevChapter(ctx context.Context, chapterID uint) (*ChapterResponse, error)
	GetNextChapter(ctx context.Context, chapterID uint) (*ChapterResponse, error)
	SearchChaptersInBook(ctx context.Context, bookID uint, keyword string, page, pageSize int) ([]*ChapterResponse, int64, error)

	// 回收站相关
	ListRecycleBin(ctx context.Context, params *RecycleBinQueryParams) (*RecycleBinListResponse, error)
	RestoreBook(ctx context.Context, id uint, restoredBy uint) (*BookResponse, error)
	RestoreChapter(ctx context.Context, id uint, restoredBy uint) (*ChapterResponse, error)
	HardDeleteBook(ctx context.Context, id uint, deletedBy uint) error
	HardDeleteChapter(ctx context.Context, id uint, deletedBy uint) error
	RestoreBookFromRecycleBin(ctx context.Context, id uint, restoredBy uint) error
	RestoreChapterFromRecycleBin(ctx context.Context, id uint, restoredBy uint) error
	BatchRestoreFromRecycleBin(ctx context.Context, req *BatchRestoreRequest, restoredBy uint) ([]uint, map[uint]string, error)
	PermanentlyDeleteFromRecycleBin(ctx context.Context, req *PermanentDeleteRequest, deletedBy uint) ([]uint, map[uint]string, error)
	ClearRecycleBin(ctx context.Context, deletedBy uint) error
}

// service 书籍应用服务实现
type service struct {
	bookService    domainBook.DomainService
	eventPublisher events.Publisher
}

// NewService 创建书籍应用服务
func NewService(bookService domainBook.DomainService, eventPublisher events.Publisher) ApplicationService {
	return &service{
		bookService:    bookService,
		eventPublisher: eventPublisher,
	}
}
